// pages/my/purchase/together/together.js
const app = getApp()
Page({
	data: {
		orderId:'',
		stockSkuId:'',
		goods:[],
		goodsType:true,
		isCompany:true,
		goodsPriceShow:true,
		hour:'',
		minutes:'',
		seconds:''
	},
	onLoad: function (options) {
  		let orderId = options.orderId,
  			stockSkuId = options.stockSkuId
  		this.setData({
  			orderId,
  			stockSkuId
  		})
  		this.getOrderDetail(orderId,stockSkuId)
  	},
  	onShareAppMessage(res) {
  		if (res.from === 'button') {
      	// 来自页面内转发按钮
      	console.log(res.target)
  		}
  		return {
  			title: this.data.goods.name,
  			path: '/pages/my/purchase/together/shareTogether?orderId='+this.data.orderId+'&stockSkuId='+this.data.stockSkuId
  		}
	},
	getOrderDetail(orderId,stockSkuId){
		let data = {
			orderId,
			stockSkuId
		}
		let that = this
		console.log(data)
		app.request('wx_app/together/getShareByOrderId',data,'GET').then(res=>{
			console.log(res)
			if (res.code==1) {
				if (res.data.togetherNumber<2) {
					let goods = []
					goods={
						url:res.data.logoUrl,
						color:res.data.tagColor,
						companyName:res.data.companyName,
						name:res.data.productName,
						price:res.data.price,
						actualPrice:res.data.price,
						type:res.data.versionValues,
						head:res.data.headPortrait,
						time:res.data.togetherCreateTime
					}
					this.setData({
						goods
					})
					setInterval(function () {
						that.timeControl(goods.time)
					}, 1000)
				}else{
					wx.switchTab({
          				url:'../../../my/my'
        			})
				}
			}else{
				app.errorMsg(res.message)
			}
		})
	},
	timeControl(time){
		let end = new Date(parseInt(time + 86400000))
		let now = new Date()
	  	//总秒数
	  	let totalSeconds = parseInt((end - now) / 1000),
	    //小时数
	    hour = Math.floor(totalSeconds / (60 * 60)),
	    //取余
	    modulo = totalSeconds % (60 * 60),
	    //分钟
	    minutes = Math.floor(modulo / 60),
	    //秒
	    seconds = modulo % 60;
	    this.setData({
	    	hour,
	    	minutes,
	    	seconds
	    })
	}
})